//
//  AppTVTopLoopView.m
//  classicFilms
//
//  Created by 刘继丹 on 17/3/14.
//  Copyright © 2017年 Stefan Arn. All rights reserved.
//

#import "AppTVTopLoopView.h"
#import "AppTVImageCCell.h"
#import "ServiceListResponse.h"

@interface AppTVTopLoopView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong) UICollectionView *mCollectionView;

@property(nonatomic, assign) AppTVLoopViewType type;

@end

@implementation AppTVTopLoopView

-(instancetype)initWithFrame:(CGRect)frame type:(AppTVLoopViewType)type{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];

        _type = type;
        
        [self addSubview:self.mCollectionView];
        
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    _mCollectionView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    if (_type == AppTVLoopViewType_top) {
        [_mCollectionView setContentInset:UIEdgeInsetsMake(10, 90, 10, 90)];
    }else if (_type == AppTVLoopViewType_BigImageBrowse) {
        [_mCollectionView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }else{
        [_mCollectionView setContentInset:UIEdgeInsetsMake(30, 90, 30, 90)];
    }
    
    if (_scrollIndex) {
        
        if (_type == AppTVLoopViewType_top) {
            [self.mCollectionView setContentOffset:CGPointMake(KVIEW_W*_scrollIndex-90-_scrollIndex*130, 0)];
        }else{
            [self.mCollectionView setContentOffset:CGPointMake(KVIEW_W*_scrollIndex, 0)];
        }
        
    }
    
}

-(void)setScrollIndex:(NSInteger)scrollIndex{
    if (_scrollIndex != scrollIndex) {
        _scrollIndex = scrollIndex;
    }
}

-(void)setImageArr:(NSArray *)imageArr{
    if (_imageArr != imageArr) {
        _imageArr = imageArr;
    }
    [self.mCollectionView reloadData];
}

-(void)setIsHidden_playBtn:(BOOL)isHidden_playBtn{
    if (_isHidden_playBtn != isHidden_playBtn) {
        _isHidden_playBtn = isHidden_playBtn;
    }
    
}

-(UICollectionView *)mCollectionView{
    if (!_mCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _mCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
        _mCollectionView.delegate = self;
        _mCollectionView.dataSource = self;
        _mCollectionView.showsHorizontalScrollIndicator = YES;
        _mCollectionView.backgroundColor = [UIColor clearColor];
        _mCollectionView.remembersLastFocusedIndexPath = YES;
        [_mCollectionView registerClass:[AppTVImageCCell class] forCellWithReuseIdentifier:@"AppTVImageCCell"];
    }
    return _mCollectionView;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _imageArr.count;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (_type == AppTVLoopViewType_top) {
        return 50;
    }else if (_type == AppTVLoopViewType_cell){
        return 60;
    }else if (_type == AppTVLoopViewType_detail || _type == AppTVLoopViewType_CaseDetail){
        return 50;
    }else{
        return 0;
    }
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    if (_type == AppTVLoopViewType_top) {
        return 50;
    }else if (_type == AppTVLoopViewType_cell){
        return 60;
    }else if (_type == AppTVLoopViewType_detail || _type == AppTVLoopViewType_CaseDetail){
        return 50;
    }else{
        return 0;
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_type == AppTVLoopViewType_top) {
        return CGSizeMake(KVIEW_W-180, 480);
    }else if (_type == AppTVLoopViewType_cell){
        if (_appleTVType == AppleTVType_sciFiHorror || _appleTVType == AppleTVType_filmNoir) {
            return CGSizeMake((KVIEW_W-300)/3.f, 280);
        }else{
           return CGSizeMake(230, 280);
        }
        
    }else if (_type == AppTVLoopViewType_detail || _type == AppTVLoopViewType_CaseDetail){
        return CGSizeMake(320, 180);
    }else{
        return CGSizeMake(KVIEW_W, KVIEW_H);
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AppTVImageCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AppTVImageCCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];

    if (_type == AppTVLoopViewType_cell) {
        
        ListModel *listModel = _imageArr[indexPath.row];
        cell.imageStr = listModel.cover;
    
    }else if(_type == AppTVLoopViewType_detail)
    {
        NSDictionary *dic = [_imageArr objectAtIndex:indexPath.row];
        cell.imageStr = [dic valueForKey:@"cover"];
        
        cell.playbtn.hidden = _isHidden_playBtn;
        
    }else if(_type == AppTVLoopViewType_BigImageBrowse){
        
        cell.imageStr = _imageArr[indexPath.row];
        
        cell.imageView.adjustsImageWhenAncestorFocused = NO;
        
    }else if (_type == AppTVLoopViewType_CaseDetail)
    {
        NSDictionary *dic = [_imageArr objectAtIndex:indexPath.row];
        cell.imageStr = [dic valueForKey:@"src_url"];

        cell.playbtn.hidden = YES;
        
    }else if (_type == AppTVLoopViewType_top)
    {
        cell.imageView.image = [UIImage imageNamed:_imageArr[indexPath.row]];
        
        cell.playbtn.hidden = YES;
        
    }else {
        
        cell.imageStr = _imageArr[indexPath.row];
        
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_imageSelectBlock) {
        _imageSelectBlock(indexPath.row);
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (_type == AppTVLoopViewType_BigImageBrowse) {
        if (_imageSelectBlock) {
            _imageSelectBlock(scrollView.contentOffset.x/(KVIEW_W-210));
        }
    }
}

- (BOOL)shouldUpdateFocusInContext:(UIFocusUpdateContext *)context {
    BOOL shouldUpdateFocusInContext = YES;
    if (context != nil) {
        if (context.focusHeading == UIFocusHeadingRight) {
            UIView *previouslyFocusedView = context.previouslyFocusedView;
            UIView *nextFocusedView = context.nextFocusedView;
            if (previouslyFocusedView != nil && nextFocusedView != nil && [previouslyFocusedView isEqual:nextFocusedView]) {
                shouldUpdateFocusInContext = NO;
            }
        }
    }
    return shouldUpdateFocusInContext;
}

- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator {
    
    [coordinator addCoordinatedAnimations:^{
        
        if (_type == AppTVLoopViewType_top) {
            
            if (context.focusHeading == 2 || context.focusHeading == 1) {
                if (_tabbarHiddenChangeBlock) {
                    _tabbarHiddenChangeBlock(context.focusHeading);
                }
            }
        }

    } completion:nil];
    
}



@end
